package com.spingbootes.kizuki.dao;

import com.alibaba.fastjson.JSON;
import com.spingbootes.kizuki.pojo.ApexInfo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class ESDaoImpl implements ESDao{

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public int insert(String index, ApexInfo info) {
        try {
            IndexRequest request = new IndexRequest(index);
            request.id(info.getId());
            request.source(JSON.toJSONString(info), XContentType.JSON);
            IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            log.info("es新增文档成功, index:{}, 结果:{}", index, response.status());
            return 1;
        } catch (Exception e) {
            log.error("es新增文档失败, index:{}, e:{}", index, e);
            return 0;
        }
    }

    @Override
    public int delete(String index, String id) {
        try {
            DeleteRequest request = new DeleteRequest(index, id);
            DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
            log.info("es删除文档成功, index:{}, 结果:{}", index, response.status());
            return 1;
        } catch (Exception e) {
            log.error("es删除文档失败, index:{}, e:{}", index, e);
            return 0;
        }
    }

    @Override
    public int update(String index, ApexInfo info) {
        try {
            UpdateRequest request = new UpdateRequest(index, info.getId());
            request.doc(JSON.toJSONString(info), XContentType.JSON);
            UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
            log.info("es更新文档成功, index:{}, 结果:{}", index, response.status());
            return 1;
        } catch (Exception e) {
            log.error("es更新文档失败, index:{}, e:{}", index, e);
            return 0;
        }
    }

    @Override
    public ApexInfo select(String index, String id) {
        try {
            GetRequest request = new GetRequest(index, id);
            GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
            log.info("es查询文档成功, index:{}, 结果:{}", index, response.getSourceAsString());
            return JSON.parseObject(response.getSourceAsString(), ApexInfo.class);
        } catch (Exception e) {
            log.error("es查询失败, index:{}, e:{}", index, e);
            return null;
        }
    }

    @Override
    public List<ApexInfo> selectAll(String index) {
        try {
            SearchRequest request = new SearchRequest(index);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            request.source(searchSourceBuilder);
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            List<ApexInfo> infos = new ArrayList<>();
            for (SearchHit hit : hits) {
                infos.add(JSON.parseObject(hit.getSourceAsString(), ApexInfo.class));
            }
            log.info("es查询文档成功, index:{}, 结果:{}", index, infos);
            return infos;
        } catch (Exception e) {
            log.error("es查询失败, index:{}", index);
            return new ArrayList<>();
        }
    }

    @Override
    public List<ApexInfo> selectDetail(String index, String detailId) {
        try {
            SearchRequest request = new SearchRequest(index);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("id", detailId);
            searchSourceBuilder.query(queryBuilder);
            request.source(searchSourceBuilder);
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            List<ApexInfo> infos = new ArrayList<>();
            for (SearchHit hit : hits) {
                infos.add(JSON.parseObject(hit.getSourceAsString(), ApexInfo.class));
            }
            log.info("es条件查询文档成功, index:{}, 结果:{}", index, infos);
            return infos;
        } catch (Exception e) {
            log.error("es条件查询失败, index:{}", index);
            return new ArrayList<>();
        }
    }


}
